//
//  WBStatusesRetweetView.swift
//  weibo
//
//  Created by yanghong on 15/12/12.
//  Copyright © 2015年 杨鸿. All rights reserved.
//

import UIKit
import SnapKit
class WBStatusesRetweetView: UIView {
    
    var bottomConstraint: Constraint?
    
    var statusesViewModel: WBStatuedViewModel? {
        didSet{

            contentLabel.text = statusesViewModel?.retweetText
            
            //判断是否有配图 ,如果有则让他显示出来
            self.bottomConstraint?.uninstall()
            if let pic_urls = statusesViewModel?.status?.retweeted_status?.pic_urls where  pic_urls.count > 0 {
                
                //设置数据
                pictureImgView.pic_urls = pic_urls
                
                pictureImgView.hidden = false
                //更新约束
                snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(pictureImgView).offset(WBStatusesCellMargin).constraint
                    
                })
                
                
            }else{
                // 让配图隐藏
                
                pictureImgView.hidden = true
                
                snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(WBStatusesCellMargin).constraint
                })
                
                
            }
            
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        addSubview(contentLabel)
        addSubview(pictureImgView)
        
        // 添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(WBStatusesCellMargin)
            make.top.equalTo(self).offset(WBStatusesCellMargin)
            
        }
        // picImgView 约束
        pictureImgView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(WBStatusesCellMargin)
            
        }
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraint = make.bottom.equalTo(pictureImgView).offset(WBStatusesCellMargin).constraint
        }
    }
    // MARK -  懒加载控件
    
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.numberOfLines = 0
        
        label.preferredMaxLayoutWidth =  SCREENWIDTH - 2 * WBStatusesCellMargin
        
        label.textColor = UIColor.darkGrayColor()
        
        return label
        
    }()
    // MARK -  懒加载配图
    
    private lazy var pictureImgView: WBStatusPictureView = {
        
        let picimgView = WBStatusPictureView()
        
        picimgView.backgroundColor = self.backgroundColor
        
        return picimgView
        
    }()
}
